package com.example.banksys;

import DataBase.DBMsg;
import com.pixelduke.control.skin.FXSkins;
import handler.queryHandler;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;

public class QueryPageController {


    public TextField accountinput;
    public PasswordField password;
    public Button queryButton;
    public Label accbalance;
    public Label acctype;
    public TableColumn<tradeTrace, Number> trade_ID;
    public TableColumn<tradeTrace, Number> money;
    public TableColumn<tradeTrace,String> type;
    public TableColumn<tradeTrace,Number> teller_ID;
    public TableColumn<tradeTrace, String> trade_date;
    public TableView<tradeTrace> tradeTraceTable;
    public Button detailed;

    public static long acc_ID;

    public void initialize(){
        accbalance.setVisible(false);
        acctype.setVisible(false);
        detailed.setDisable(true);
        accountinput.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent keyEvent) {
                if(accountinput.getText().isEmpty()||accountinput.getText().equals("")){
                    accbalance.setVisible(false);
                    acctype.setVisible(false);
                    detailed.setDisable(true);
            }
        }});
    }

    /********************************************************************************************
    绑定到查询按钮，首先进行账户状态查询，然后进行交易记录查询
    ***/
    public void Query() throws SQLException {
        queryHandler queryhandler=new queryHandler();
        ObservableList<tradeTrace> resultList= FXCollections.observableArrayList();
        Account currentAccount=new Account();

        try{
            long accountID=Long.parseLong(accountinput.getText());
            this.acc_ID=accountID;
            currentAccount.setAccount_ID(accountID);
            currentAccount.setPassword(password.getText());
        }catch (NumberFormatException e){
            /*
             这里处理一下没办法转换的情况不然要报错
             */
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.titleProperty().set("信息");
            alert.headerTextProperty().set("账户ID中不能包含除了数字以外的其他字符！");
            alert.showAndWait();
            return;
        }
        DBMsg ret=queryhandler.accountQuery(currentAccount);
        if(ret.getState()==DBMsg.State.ERROR){
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.titleProperty().set("信息");
            alert.headerTextProperty().set("账户信息："+ret.getInfo());
            alert.showAndWait();
            return;
        }
        acctype.setText(currentAccount.getType());
        accbalance.setText(currentAccount.getBalance()+"");
        acctype.setVisible(true);
        accbalance.setVisible(true);

        /*
         * 交易记录查询
         */
        DBMsg resMsg=queryhandler.tradeQuery(currentAccount,resultList);
        if(resMsg.getState()== DBMsg.State.ERROR){
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.titleProperty().set("信息");
            alert.headerTextProperty().set(resMsg.getInfo());
            alert.showAndWait();
        }else{
            /********************************************
             * 关键问题，这其中所有的相关数字的类似SimpleLongProperty,SimpleDoubleProperty之类的，
             * 其父类均是ObservableValue<Number>,不是类似ObservableValue<Long&Double>
             * 为了让程序识别，必须在泛型的地方使用number
             * 但是，String就不需要
             * 关键在于查看继承的父类的类型
             * 所以，还是要看文档详细了解这些类的继承体系，作为simpleProperty正是继承了，实现了ObservableValue类的方法才能够向下造型，所以继承关系搞错将会不能够产生多态
             *********************************************/
            tradeTraceTable.setItems(resultList);
            trade_ID.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeTrace, Number>, ObservableValue<Number>>() {
                @Override
                public ObservableValue<Number> call(TableColumn.CellDataFeatures<tradeTrace, Number> tradeTraceNumberCellDataFeatures) {
                    return tradeTraceNumberCellDataFeatures.getValue().getTrade_ID();
                }
            });
            money.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeTrace, Number>, ObservableValue<Number>>() {
                @Override
                public ObservableValue<Number> call(TableColumn.CellDataFeatures<tradeTrace, Number> tradeTraceNumberCellDataFeatures) {
                    return tradeTraceNumberCellDataFeatures.getValue().getMoney();
                }
            });
            type.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeTrace, String>, ObservableValue<String>>() {
                @Override
                public ObservableValue<String> call(TableColumn.CellDataFeatures<tradeTrace, String> tradeTraceStringCellDataFeatures) {
                    return tradeTraceStringCellDataFeatures.getValue().getType();
                }
            });
            teller_ID.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeTrace, Number>, ObservableValue<Number>>() {
                @Override
                public ObservableValue<Number> call(TableColumn.CellDataFeatures<tradeTrace, Number> tradeTraceNumberCellDataFeatures) {
                    return tradeTraceNumberCellDataFeatures.getValue().getTeller_ID();
                }
            });
            /*
            * timeStamp也没有办法转换成ObervableValue，必须用StringProperty
            */
            trade_date.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<tradeTrace, String>, ObservableValue<String>>() {
                @Override
                public ObservableValue<String> call(TableColumn.CellDataFeatures<tradeTrace, String> tradeTraceStringCellDataFeatures) {
                    String t=tradeTraceStringCellDataFeatures.getValue().getTrade_date().toString();
                    return new SimpleStringProperty(t);
                }
            });
            detailed.setDisable(false);
        }
    }

    public void detail () throws IOException {
        FXMLLoader fxmlLoader=new FXMLLoader(MainApplication.class.getResource("subAccountDetail.fxml"));
        Scene scene=new Scene(fxmlLoader.load());
        scene.getStylesheets().add(FXSkins.getStylesheetURL());
        SubAccountDetailController t=fxmlLoader.getController();
        t.setAccount_ID(this.acc_ID);
        Stage stage=new Stage();
        stage.setScene(scene);
        stage.show();
    }

    public void generlize() throws IOException {
        FXMLLoader fxmlLoader=new FXMLLoader(MainApplication.class.getResource("listOption.fxml"));
        Scene scene=new Scene(fxmlLoader.load());
        scene.getStylesheets().add(FXSkins.getStylesheetURL());
        Stage stage=new Stage();
        stage.setScene(scene);
        stage.show();
    }

    public void diagram() throws IOException {
        FXMLLoader fxmlLoader=new FXMLLoader(MainApplication.class.getResource("pieCharts.fxml"));
        Scene scene=new Scene(fxmlLoader.load());
        scene.getStylesheets().add(FXSkins.getStylesheetURL());
        Stage stage=new Stage();
        stage.setScene(scene);
        stage.show();
    }
 }
